<?php
require_once '../conn.php';
$json = file_get_contents('php://input');/* 获取json数据 */
$data = json_decode($json, true);/* 解析成数组 */
$optType = $data['optType'];/* 获取需要的值 */
// $password = password_hash($remark, PASSWORD_DEFAULT);//使用`password_hash()`函数加密密码
if($optType == 'login'){
  $userName = $data['userName'];
  $userPassword = $data['userPassword'];
  $ip = $data["ip"];
  $address = $data['address'];
  $way = $data["way"];
  $remark = $data["remark"];
  if(empty($userName) && empty($userPassword)){
    header('Content-Type: application/json');echo json_encode(['code' => 400,'message'=>'数据请求错误！']);
  }else{
    $userType = 'admin';
    $ver = $conn->prepare("SELECT userPasswd FROM sysaccount WHERE userAccount = ? AND userType = ? LIMIT 1");
    $ver->bind_param("ss", $userName, $userType);
    if($ver->execute()===TRUE){
      $resuPass = $ver->get_result();
      $numRows = $resuPass->num_rows;
      if($numRows > 0){
        while($row = $resuPass->fetch_assoc()){$selPassword = $row['userPasswd'];}
        if(!password_verify($userPassword,$selPassword)){
          header('Content-Type: application/json');echo json_encode(['code' => 400,'message'=>'账号或密码错误！']);
        }else{
          $loginTimeStamp = time();
          $up = $conn->prepare("UPDATE sysaccount SET loginTimeStamp = ? WHERE userAccount = ?");
          $up->bind_param('is',$loginTimeStamp,$userName);
          $up->execute();
          $sel = $conn->prepare("SELECT id,userNickName,userAccount,wx_appid,wx_token,wx_sex,userType,loginTimeStamp,loginTime,creatTime FROM sysaccount WHERE userAccount = ? AND userType = ? LIMIT 1");
          $sel->bind_param("ss", $userName, $userType);
          if($sel->execute() === TRUE){
            $result = $sel->get_result();
            $res = $result->fetch_assoc();
            $logs = $conn->prepare("INSERT INTO sys_logs (userName,ip,address,way,remark)VALUES(?,?,?,?,?)");
            $logs->bind_param('sssss',$userName,$ip,$address,$way,$remark);
            if($logs->execute()===TRUE){
              header('Content-Type: application/json');echo json_encode(['code' => 200,'data'=>$res,'message'=>'登录成功！']);
            }
          }
        }
      }else{header('Content-Type: application/json');echo json_encode(['code' => 400,'message'=>'用户不存在！']);}
    }
  }
}else if($optType == 'register'){
  $reCodeData = $data['reCodeData'];
  $reCode = $conn->prepare("SELECT registerStatus,registerCode FROM sys_config WHERE id = ?");
  $selId = 3306;
  $reCode->bind_param('i', $selId);
  if($reCode->execute()===TRUE){
    $resuRegister = $reCode->get_result();
    while($row = $resuRegister->fetch_assoc()){$registerCode = $row['registerCode'];$registerStatus = $row['registerStatus'];}
    if($registerStatus == 'true'){
      if($registerCode == $reCodeData){
        $userNickName = $data['userNickName'];
        $userAccount = rand(111111,999999);
        $userPasswd = password_hash($data['userPassword'], PASSWORD_DEFAULT);
        $userType = 'admin';
        $sel = $conn->prepare("SELECT * FROM sysaccount WHERE userAccount = ?");
        $sel->bind_param("s",$userAccount);
        if($sel->execute()===TRUE){
          $numResult = $sel->get_result();
          $numRows = $numResult->num_rows;
          if($numRows > 0){
            $userAccount = rand(111111,999999);
          }
          $init = $conn->prepare("INSERT INTO sysaccount (userNickName,userAccount,userPasswd,userType)VALUES(?,?,?,?)");
          $init->bind_param("ssss",$userNickName,$userAccount,$userPasswd,$userType);
          if($init->execute()===TRUE){
            $loginTimeStamp = time();
            $up = $conn->prepare("UPDATE sysaccount SET loginTimeStamp = ? WHERE userAccount = ?");
            $up->bind_param('is',$loginTimeStamp,$userAccount);
            $up->execute();
            $sel = $conn->prepare("SELECT id,userNickName,userAccount,wx_appid,wx_token,wx_sex,userType,loginTimeStamp,loginTime,creatTime FROM sysaccount WHERE userAccount = ? AND userType = ? LIMIT 1");
            $sel->bind_param("ss", $userAccount, $userType);
            if($sel->execute() === TRUE){
              $result = $sel->get_result();
              $res = $result->fetch_assoc();
              header('Content-Type: application/json');echo json_encode(['code' => 200,'data'=>$res,'message'=>'注册成功！账号为：'.$userAccount]);
            }
          }
        }
      }else{header('Content-Type: application/json');echo json_encode(['code' => 400,'message'=>'注册失败，原因：注册码错误！']);}
    }else{header('Content-Type: application/json');echo json_encode(['code' => 400,'message'=>'注册失败，原因：未开放注册或注册码错误！']);}
  }
}else{
  header('Content-Type: application/json');echo json_encode(['code' => 400,'message'=>'数据请求错误！']);
}